﻿using UnityEditor;

using UnityEngine;

namespace YuoTools
{
    [CustomPropertyDrawer(typeof(BackgroundColorAttribute))]
    public class BackgroundColorAttributeDrawer : PropertyDrawer
    {
        public override void OnGUI(Rect position, SerializedProperty property, GUIContent label)
        {
            var backgroundColorAttribute = attribute as BackgroundColorAttribute;

            bool doHighlight = true;

            if (doHighlight)
            {
                var color = backgroundColorAttribute.color;
                var padding = EditorGUIUtility.standardVerticalSpacing;
                var highlightRect = new Rect(position.x - padding, position.y - padding,
                    position.width + (padding * 2), position.height + (padding * 2));
                EditorGUI.DrawRect(highlightRect, color);
                var cc = GUI.contentColor;
                GUI.contentColor = backgroundColorAttribute.textColor;
                EditorGUI.PropertyField(position, property, label);

                GUI.contentColor = cc;
            }
            else
            {
                EditorGUI.PropertyField(position, property, label);
            }
        }

        public override float GetPropertyHeight(SerializedProperty property, GUIContent label)
        {
            return EditorGUI.GetPropertyHeight(property, label, true);
        }
    }
}